import {GoodsTypes, PlaceWays} from "../enums/enum.js";
import {constants} from "../constants.js";
import {PlumableGoods} from "./plumableGoods.js";

const {MAP_WIDTH, MAX_LIGHT_POWER} = constants

export class Light extends PlumableGoods {

  /**
   * 能量，代表光能扩散几格，默认 5 格
   */
  power = 5


  constructor(obj) {
    super(obj);
    this.type = GoodsTypes.LIGHT
    this.power = obj.power
  }


  put = (fog,x,y) => {
    for (let i = 1; i <= this.power; i++) {
      let ny = y - this.power + i
      for (let j = -i + 1; j < i; j++) {
        fog[x + j + ny * MAP_WIDTH] += Math.floor((i - Math.abs(j)) * MAX_LIGHT_POWER / this.power)
      }
    }

    for (let i = 1; i < this.power; i++) {
      let ny = y + this.power - i
      for (let j = -i + 1; j < i; j++) {
        fog[x + j + ny * MAP_WIDTH] += Math.floor((i - Math.abs(j)) * MAX_LIGHT_POWER / this.power)
      }
    }
  }


  /**
   * 移除光源
   * @param fog {Uint8Array} 用于存放迷雾的arr
   * @param x {number}
   * @param y {number}
   */
  remove = (fog, x, y) => {
    for (let i = 1; i <= this.power; i++) {
      let ny = y - this.power + i
      for (let j = -i + 1; j < i; j++) {
        fog[x + j + ny * MAP_WIDTH] -= Math.floor((i - Math.abs(j)) * MAX_LIGHT_POWER / this.power)
      }
    }

    for (let i = 1; i < this.power; i++) {
      let ny = y + this.power - i
      for (let j = -i + 1; j < i; j++) {
        fog[x + j + ny * MAP_WIDTH] -= Math.floor((i - Math.abs(j)) * MAX_LIGHT_POWER / this.power)
      }
    }
  }
}
